package com.pirestupppai.widgets.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.pirestupppai.R;
import com.pirestupppai.common.Constant;
import com.pirestupppai.ui.activity.UserActivity;
import com.pirestupppai.utils.ImageLoadUtil;

/**
 * 封装逻辑的头像控件
 * 不用CircleImageView, 用Glide原生圆形控件方法
 *
 * @author: xiaoluo
 * @date: 2017-03-07 09:53
 */
public class AvatarView extends RelativeLayout implements ImageLoadUtil.ImageArea{
    private Context mContext;
    private ImageView mAvatarIv;
    private ImageView mVipIv;
    private int mUid = -1;
    private boolean isVip = false;

    public AvatarView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public AvatarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.widget_avatar, this, true);
        mAvatarIv = (ImageView) this.findViewById(R.id.avatar_iv);
        mVipIv = (ImageView) this.findViewById(R.id.avatar_vip_icon);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 自定义操作
                if (mUid != -1) {
                    Intent intent = new Intent(mContext, UserActivity.class);
                    intent.putExtra(Constant.IntentKey.UID, mUid);
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        RelativeLayout.LayoutParams imageParams = (LayoutParams) mAvatarIv.getLayoutParams();
        if (imageParams != null) {
            imageParams.width = getMeasuredWidth();
            imageParams.height = getMeasuredWidth();
            mAvatarIv.setLayoutParams(imageParams);
        }
        int vWidth = getMeasuredWidth() / 3;
        int vHeight = getMeasuredWidth() / 3;
        RelativeLayout.LayoutParams vParams = (LayoutParams) mVipIv.getLayoutParams();
        if (vParams != null) {
            vParams.width = vWidth;
            vParams.height = vHeight;
            mVipIv.setLayoutParams(vParams);
        }
    }

    public void setAvatarImage(int resId) {
        mAvatarIv.setImageResource(resId);
    }

    public void setAvatarImage(Bitmap bitmap) {
        mAvatarIv.setImageBitmap(bitmap);
    }

    public void setAvatarImage(Drawable drawable) {
        mAvatarIv.setImageDrawable(drawable);
    }

    /**
     * 设置uid和vip，vip默认为false，接口没给直接传false
     */
    public void setUid(int uid, boolean isVip) {
        this.mUid = uid;
        this.isVip = isVip;

        if (isVip) {
            mVipIv.setVisibility(VISIBLE);
        } else {
            mVipIv.setVisibility(GONE);
        }
        postInvalidate();
    }

    @Override
    public ImageView getImage() {
        return mAvatarIv;
    }
}
